草庐IT

python - 构建处理 redis 和 cache_method 装饰器的测试

全部标签

ruby-on-rails - 摩卡 : How to add expectation of a method when there are multiple invocations with different parameters

我有一个RailsControllerAction要测试。在那个Action中,一个方法User.can?使用不同的参数多次调用。在其中一个测试用例中,我想确保User.can?('withdraw')被调用。但我不关心User.can的调用?与其他参数。defaction_to_be_tested...@user.can?('withdraw')...@user.can?('deposit')...end我在测试中尝试了以下:User.any_instance.expects(:can?).with('withdraw').at_least_once.returns(true)但是测

ruby-on-rails - 使用 define_method 动态定义 setter 方法?

我使用大量迭代来定义模型中的便捷方法,例如:PET_NAMES.eachdo|pn|define_method(pn)do......end但我从来没有能够动态定义setter方法,即:defpet_name=(name)...end像这样使用define_method:define_method("pet_name=(name)")do...end有什么想法吗?提前致谢。 最佳答案 这是一个在用于扩展类的模块中使用define_method的完整示例:moduleVerboseSetterdefmake_verbose_sette

ruby-on-rails - 无方法错误 : undefined method `on' for main:Object

当我尝试bundleexeccapproductiondeploy--trace时,我收到一条错误消息:deploy@h2540559:/www/apps/foodsoft$bundleexeccapproductiondeploy--trace**Invokeproduction(first_time)**Executeproduction**Invokeload:defaults(first_time)**Executeload:defaults**Invokervm:hook(first_time)**Executervm:hookcapaborted!NoMethodError

ruby - 什么是 Rack ?我可以使用它通过 Ruby 构建 Web 应用程序吗?

ruby新手警告!(嘿,押韵:))我已经阅读了官方定义,但还是空手而归。他们所说的中间件到底是什么?目的是使用带https的ruby​​吗?patnaik博客上的小教程让事情变得更清晰,但我如何在本地主机上使用它做一些事情?我安装了ruby​​1.9.2以及rackgem和mongrel服务器。我应该先启动mongrel吗?怎么办? 最佳答案 只是添加对Rack的简单解释(因为我觉得缺少):Rack基本上是网络应用程序与网络服务器通信的一种方式。通信是这样的:Web服务器将环境告知应用程序-这主要包含用户作为请求发送的内容-url、

ruby - FactoryBot 构建具有特征的对象列表

我在测试中使用factory_bot创建对象,这里是我的工厂示例:factory:userdoname"John"surname"Doe"trait:with_photodoignoredophoto_count1endafter(:create)do|user,evaluator|FactoryBot.create_list(:photo,evaluator.photo_count)endendend所以我可以用这样的照片创建一个用户:FactoryBot.create(:user,:with_photo)或者没有照片:FactoryBot.create(:user)或者创建一个用户

ruby - 您如何使用 ptrace 对在 Rails 中运行的不受信任的代码进行沙盒处理?

假设我有这个糟糕的Controller代码:classMovesController一段时间以来,我一直在寻找一种最好的方法来沙箱化不受信任的代码的执行,并偶然发现了这个ruby​​-lang特性中的讨论:https://bugs.ruby-lang.org/issues/8468TherealsolutiontothisproblemistorunasandboxatthelevelaboveRuby.Irununtrustedcodeonhttp://eval.ininsideaptracebasedsandbox.CharlieSomerville对该主题的进一步研究并没有得到比

ruby - Python 程序员通过学习 Ruby 可以获得什么?

我打算在大学学习Ruby、Haskell和Prolog。现在,我想知道什么应该引起我的大部分注意。我有半年的时间来完成这三门类(class),这意味着我需要选择一种语言来获得我的课外时间。其他我将学习的内容足以在类(class)中做得很好。我对Haskell和Prolog非常熟悉,知道学习它们会教会我一些非常重要的计算机科学概念。我不太确定ruby。通过一些教程和介绍,我觉得ruby​​是很多肤浅的魔法。现在我问ruby​​的人:如果我决定不再使用它,我会得到什么,在我花了半年时间学习它之后,Python还没有教我。虽然我意识到这是一个很有争议的潜在话题,但这个问题并不是为了“证明”r

ruby - 什么时候用undef_method,什么时候用remove_method?

我想重新定义一个方法,但要避免与之相关的警告。我应该使用undef_method还是remove_method来这样做?(是的,重新定义方法有点老套。我这样做是因为我有一些内存,我想在单元测试运行时使用,而不是在程序本身运行时使用。) 最佳答案 来自finemanual:undef_method(symbol)→selfPreventsthecurrentclassfromrespondingtocallstothenamedmethod.Contrastthiswithremove_method,whichdeletestheme

ruby-on-rails - 无方法错误 : undefined method `safe_constantize'

我有一个简单的模型:classPost它有一个名为type的INT列当我创建记录时:Post.create(:type=>1)我得到:NoMethodError:undefinedmethod`safe_constantize'for1:Fixnum我做错了什么? 最佳答案 就像这样:在Fixnum上没有名为safe_constantize的方法。type用于SingleTableInheritance.您通常会在应用程序中将已知模型的字符串表示形式作为type值。参见railsguides

ruby-on-rails - 切换到 Rails 4.1 时出现错误 - `method_missing':ActiveRecord::Base:Class (NoMethodError) 的未定义方法 `whitelist_attributes='

从Rails4.0切换到Rails4.1时出现此错误:activerecord-4.1.8/lib/active_record/dynamic_matchers.rb:26:in`method_missing':undefinedmethod`whitelist_attributes='forActiveRecord::Base:Class(NoMethodError)我没有在我的应用程序的任何地方使用attr_accessible或attr_protected所以我想知道为什么我有问题。当迁移到Rails4.0时,我已经安装了我的application.rb:配置/应用程序.rb:c